package 晚间作业0421

/**
 * 实战案例三
 */
object Scala_03_exp03 {
  abstract class Item
  case class Article(description: String, price: Double) extends Item

  //discount 表示折扣
  case class Bundle(description: String, discount: Double, items: Item*) extends Item

  val special = Bundle("Father's day special", 20.0,
    Article("Scala for the Impatient", 39.95),
    Bundle("Anchor Distillery Sampler", 10.0,
      Article("Old Potrero Straight Rye Whiskey", 79.95),
      Article("Junípero Gin", 32.95)))

  /**
   * 打印item价格
   */
  def getItemOriginalPrice(items: Item): Double = {
    items match {
      case Article(_, price) => price
      case Bundle(_, discount, items@_*) =>
        items.map(item => getItemOriginalPrice(item)).sum - discount
      case _ => 0
    }
  }

  def main(args: Array[String]): Unit = {
    println(getItemOriginalPrice(special))
  }
}

/**
 * 课后作业05
 */
object Scala_03_05 {
  def swap(x: (Int, Int)):(Int, Int) = {
    x match {
      case (x, y) => (y, x)
    }
  }

  def main(args: Array[String]): Unit = {
    println(swap((1, 2)))
  }
}

/**
 * 课后作业06
 */
object Scala_03_06 {
  def swap(array: Array[Int]): Array[Int] = {
    array match {
      case Array(x, y, tail @_*) => Array(y, x) ++ tail
    }
  }
  def swap(list: List[Int]): List[Int] = {
    list match {
      case x :: y :: tail => y :: x :: tail
    }
  }

  def main(args: Array[String]): Unit = {
    println(swap(Array(1, 2, 3, 4)).mkString("Array(", ", ", ")"))
    println(swap(List(1, 2, 3, 4)))
  }
}


